package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.Rolemenu;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.service.RolemenuService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.sso.model.Role;
import com.jframe.sso.model.UserGroup;
import com.jframe.sso.service.RoleService;
import com.jframe.sso.service.UserGroupService;
import com.jframe.sys.controller.utils.BaseAction;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Tree;



public class RolemenuAction extends BaseAction{
	
	public void list(){
		//获取基本参数
		int roleId = StringUtil.parseInt(this.request.getParameter("roleId"));
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		RolemenuService rolemenuService = (RolemenuService)this.getBean("rolemenuService");
		//获取角色
		if(roleId==0){
			RoleService roleService = (RoleService)this.getBean("roleService");	
			Role role = roleService.load(this.getCurUser().getRoleId());
			roleId=role.getId();
		}
		//进行权限分配
		Rolemenu rolemenu = rolemenuService.load("roleId",roleId);
		List<Sysmenu> ls = sysmenuService.findListByGroupId(groupId);
		for(Sysmenu sysmenu:ls){
			if(sysmenu.getActive()!=1){
				continue;
			}
			Map<String,Object> map = new HashMap<String,Object>();
			if(sysmenu.getId()==1){
				continue;
			}
			map.put("id", sysmenu.getId());
			map.put("name", sysmenu.getText());
			map.put("pId",sysmenu.getUpId());
			String[] menuIds = StringUtil.null2Space(rolemenu.getMenuIds()).split(",");
			boolean flag = false;
			for(String menuId:menuIds ){
				if(menuId.equals(""+sysmenu.getId())){
					flag=true;
				}
			}
			if(flag){
				map.put("checked", true);
			}else{
				map.put("checked", false);
			}
			if(sysmenu.getLeaf()==0){
				map.put("open",true);
			}
			items.add(map);
		}
		String json = JsonUtil.list2Json(items);
		this.print(json);
	}
	
	private List<Map<String,Object>> getChildNode(String menuIds,int upId,int groupId){
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> ls =  sysmenuService.findListByUpIdAndGroupId(upId,groupId);
		for(Sysmenu sysmenu:ls){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", sysmenu.getId());
			map.put("text", sysmenu.getText());
			map.put("iconCls", sysmenu.getIconCls());
			String[] menus = menuIds.split(",");
			boolean flag = false;
			for(String menuId:menus ){
				if(menuId.equals(""+sysmenu.getId())){
					flag=true;
				}
			}
			if(flag){
				map.put("checked", true);
			}else{
				map.put("checked", false);
			}
			if(sysmenu.getLeaf()==0){
				map.put("state", "open");
				map.put("children", this.getChildNode(menuIds,sysmenu.getId(),groupId));
			}
			items.add(map);
		}
		return items;
	}
	
	public List<UserGroup> getUserGroups(){
		UserGroupService userGroupService = (UserGroupService)this.getBean("userGroupService");
		List<UserGroup> ls = userGroupService.findList("where active=1 order by id");
		return ls;
	}
	
	public void getRightTrees(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		RoleService roleService = (RoleService)SpringUtil.getBean("roleService");
		List<Role> treels = roleService.findByGroupId(groupId);
		List<Tree> ls = new ArrayList<Tree>();
		for(Role role:treels){
			Tree tree = new Tree();
			tree.setId(""+role.getId());
			tree.setText(role.getName());						
			ls.add(tree);
		}
		this.print(JsonUtil.list2Json(ls));
	}
	
	public void save(){
		int roleId = StringUtil.parseInt(this.request.getParameter("roleId"));
		String ids = StringUtil.null2Space(this.request.getParameter("ids"));
		RolemenuService rolemenuService = (RolemenuService)this.getBean("rolemenuService");
		Rolemenu rolemenu = rolemenuService.load("roleId", roleId);
		rolemenu.setMenuIds(ids);
		rolemenu.setRoleId(roleId);
		rolemenuService.save(rolemenu);
		this.print(true);
	}

}
